<?php

class Admin_CategorieController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $categorie = new Admin_Model_DbTable_Categories();
        $this->view->categories = $categorie->getAllCaterories();

    }

    public function addAction()
    {
        $form = new Admin_Form_Categorie();
        $this->view->form = $form;
        
        $request = $this->getRequest();
        if ($request->isPost()){
        	$formData = $request->getPost();
        	
        	if ($form->isValid($formData)){
        		$name = $form->getValue('name');
        		
        		$categorie = new Admin_Model_DbTable_Categories();
        		$categories = $categorie->addCategorie($name);
        		
        		$this->_redirect('admin/categorie');
        	}else {
        		$form->populate($formData);
        	}
        }
    }

    public function editeAction()
    {
        $form = new Admin_Form_Categorie();
        $form->submit->setLabel('Sauvegarder');
        $this->view->form = $form;
        
        if ($this->getRequest()->isPost()){
        	$formData = $this->getRequest()->getPost();
        	if ($form->isValid($formData)){
				$id = $this->_getParam('id', 0);
        		$name= $form->getValue('name');

        		$categorie = new Admin_Model_DbTable_Categories();
        		$categories = $categorie->editeCategorie($id, $name);

        		$this->_redirect('admin/categorie');
        	} else {
        		$form->populate($formData);
        	}
        } else {
        	$id = $this->_getParam('id', 0);
        	if ($id > 0){
        		$categorie = new Admin_Model_DbTable_Categories();
        		$form -> populate($categorie->obtenirCategorie($id));
        	}
        }
    }
    
    public function deleteAction()
    {
        	if ($this->getRequest()->isPost()){
    		$supprimer = $this->getRequest()->getPost('supprimer');

    		if ($supprimer == 'Oui'){	    		
		    	// récupération du paramettre ID
		    	$id = $this->getRequest()->getPost('id');	
		    	
		    	//suppression du produit en question
		    	$categories = new Admin_Model_DbTable_Categories();
		    	$categories->deleteCategorie($id);
    		}
	    	
	    	$this->_redirect("/admin/categorie");
	    		    	
    	}else {
    		$id = $this->_getParam('id', 0);
    		$categorie = new Admin_Model_DbTable_Categories();
    		$this->view->categorie = $categorie->obtenirCategorie($id);
    	}
    }

    public function allAction()
    {
        // action body
    }


}
